# -*- coding: utf-8 -*-

# --------------------------------------------------------------------------------------------------------------------------------
# Class:     Img2Python
# Platform:  WINNT
# Comments:  Convert all ico to python file for the selected folder
# User:      JustDoIT
# Date:      6th Dec, 2016
# --------------------------------------------------------------------------------------------------------------------------------

import wx, os, sys, subprocess
from wx.tools.img2py import img2py

# --------------------------------------------------------------------------------------------------------------------------------
# Class: Img2Python
# --------------------------------------------------------------------------------------------------------------------------------
class Img2Python(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Img2Python, self).__init__(*args, **kwargs)

        self.Size = wx.Size(550, 435)
        # self.SetIcon(img.app.getIcon())

        self.InitUI()

        self.Centre()
        self.Show()

    def InitUI(self):

        panel = wx.Panel(self)

        sizer = wx.GridBagSizer(5, 5)

        text1 = wx.StaticText(panel, label="Image to python")
        text1.SetFont(wx.Font(16, wx.DEFAULT, wx.ITALIC, wx.NORMAL))
        sizer.Add(text1, pos=(0, 0), span=(1, 4), flag=wx.LEFT | wx.TOP | wx.RIGHT, border=5)

        # icon = wx.StaticBitmap(panel, bitmap=img.conf32.getBitmap())
        # sizer.Add(icon, pos=(0, 4), flag=wx.TOP | wx.RIGHT | wx.ALIGN_RIGHT, border=5)

        line = wx.StaticLine(panel)
        sizer.Add(line, pos=(1, 0), span=(1, 5), flag=wx.EXPAND | wx.BOTTOM, border=0)

        # -------------------------------------------------------------------------------------------------------------------------------
        self.txtdir = wx.TextCtrl(panel, value='Select directory ...', style=wx.TE_LEFT | wx.TE_READONLY)
        self.btndir = wx.Button(panel, wx.ID_HOME, label='Browser')

        sizer.Add(self.txtdir, pos=(2, 0), span=(1, 4), flag=wx.LEFT | wx.TOP | wx.EXPAND, border=5)
        sizer.Add(self.btndir, pos=(2, 4), flag=wx.TOP | wx.EXPAND, border=5)
        self.btndir.Bind(wx.EVT_BUTTON, self.OnButton)

        # Attribute choice -------------------------------------------------------------------------------------------------------------
        attbox = wx.BoxSizer(wx.HORIZONTAL)
        nflag = wx.CheckBox(panel, label='-n')
        mflag = wx.CheckBox(panel, label='-m')
        iflag = wx.CheckBox(panel, label='-i')
        fflag = wx.CheckBox(panel, label='-f')

        attbox.Add(nflag, flag=wx.LEFT | wx.RIGHT, border=5)
        attbox.Add(mflag, flag=wx.LEFT | wx.RIGHT, border=15)
        attbox.Add(iflag, flag=wx.LEFT | wx.RIGHT, border=15)
        attbox.Add(fflag, flag=wx.LEFT | wx.RIGHT, border=15)

        sizer.Add(attbox, pos=(3, 0), span=(1, 5), flag=wx.TOP | wx.BOTTOM | wx.EXPAND, border=0)

        # ------------------------------------------------------------------------------------------------------------------------------
        sb = wx.StaticBox(panel, label='File Log')
        sboxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL)

        self.outstr = wx.TextCtrl(panel, size=(525, 160),
                                  style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.VSCROLL)

        sboxsizer.Add(self.outstr, proportion=1, flag=wx.EXPAND)

        sizer.Add(sboxsizer, pos=(4, 0), span=(1, 5), flag=wx.LEFT | wx.EXPAND | wx.BOTTOM, border=5)

        line2 = wx.StaticLine(panel)
        sizer.Add(line2, pos=(6, 0), span=(1, 5), flag=wx.BOTTOM | wx.EXPAND, border=5)

        # ------------------------------------------------------------------------------------------------------------------------------
        btnhelp = wx.Button(panel, 301, label='Open')
        btnok = wx.Button(panel, 302, label='OK')
        btncancel = wx.Button(panel, 303, label='Cancel')

        sizer.Add(btnhelp, pos=(7, 0), flag=wx.EXPAND | wx.BOTTOM, border=10)
        sizer.Add(btnok, pos=(7, 3), flag=wx.EXPAND | wx.BOTTOM, border=10)
        sizer.Add(btncancel, pos=(7, 4), flag=wx.EXPAND | wx.BOTTOM, border=10)

        btnhelp.Bind(wx.EVT_BUTTON, self.OnButton)
        btnok.Bind(wx.EVT_BUTTON, self.OnButton)
        btncancel.Bind(wx.EVT_BUTTON, self.OnButton)

        # ------------------------------------------------------------------------------------------------------------------------------
        sizer.AddGrowableCol(0)
        sizer.AddGrowableRow(4)

        panel.SetSizer(sizer)

    def OnButton(self, e):

        eid = e.GetId()

        if eid == wx.ID_HOME:

            dlg = wx.DirDialog(self, 'Select forder', style=wx.DD_DEFAULT_STYLE)
            if dlg.ShowModal() == wx.ID_OK:
                self.txtdir.SetValue(dlg.GetPath())
                print self.txtdir

            dlg.Destroy()

        elif eid == 301:

            if os.path.isdir(self.txtdir.GetValue()):
                subprocess.Popen('explorer ' + self.txtdir.GetValue())
            else:
                wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR)

        elif eid == 302:
            if os.path.isdir(self.txtdir.GetValue()):
                self.forderprocess()
            else:
                wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR)
        elif eid == 303:
            self.Close()
        else:
            e.Skip()

    def forderprocess(self):
        folder = self.txtdir.GetValue()
        listext = ('.png', '.ico', '.icon', '.gif')

        pyfile = folder + '\img.py'

        if os.path.isdir(folder):

            self.outstr.AppendText(
                'Starting.................................................................................\n\n')

            for name in os.listdir(folder):
                (x, ext) = os.path.splitext(name)

                if ext in listext:
                    img = os.path.join(folder, name)

                    fs = open('log.txt', 'w')
                    temp = sys.stdout
                    sys.stdout = fs

                    self.outstr.AppendText('Name :' + x)

                    if os.path.isfile(pyfile):
                        ret = img2py(img, pyfile, append=True)
                    else:
                        ret = img2py(img, pyfile, append=True)

                    sys.stdout = temp
                    fs.close()
                    str = open('log.txt', 'r').read()
                    self.outstr.AppendText(str + '\n')

            self.outstr.AppendText(
                'Done ....................................................................................\n')


# --------------------------------------------------------------------------------------------------------------------------------
# Class: APP
# --------------------------------------------------------------------------------------------------------------------------------
class App(wx.App):
    def OnInit(self):
        defaultstyle = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.FRAME_NO_TASKBAR
        Img2Python(None, title='亿创图片转BASE64工具', style=defaultstyle)
        return True


# --------------------------------------------------------------------------------------------------------------------------------
# Function main()
# --------------------------------------------------------------------------------------------------------------------------------
def main():
    App().MainLoop()


if __name__ == '__main__':
    main()
